from rest_framework.views import APIView

from rest_framework.response import Response
from django_redis import get_redis_connection

from .utils import encrypt,verify_token
from django.conf import settings

class LoginView(APIView):
    def post(self, request):
        #创建redis
        cache = get_redis_connection('default')
        print(request.data)
        data = {
            'username': request.data.get('username'),
            'password': request.data.get('password') 
        }
        #加密用户
        token = encrypt(data)
        request.session['token'] = token
        cache.set(token, token,ex=settings.JWT_EXPIRE)
        print(token)
        return Response({'token': token})